package com.cloud.wms.ui.returned.register;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.tools.PlayVoiceManager;
import com.cloud.core.tools.VolumeManager;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.returned.contact.ReturnRegisterContact;
import com.cloud.wms.ui.returned.entity.ExpressDeliveryEntity;
import com.cloud.wms.ui.returned.presenter.ReturnRegisterPresenter;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by FC on 2021/5/13.
 * Desc：
 */
public class ReturnRegisterActivity extends BaseBarListActivity<ExpressDeliveryEntity, ReturnRegisterPresenter> implements ReturnRegisterContact.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    ExpressDeliveryEntity deliveryEntity;
    String trackingNumber;

    @Override
    protected String barTitle() {
        return "退货登记";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_return_register;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_return_register;
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        trackingNumber = result;
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            if (result.equalsIgnoreCase(mAdapter.getItem(i).getTrackingNumber())) {
                info("该面单已添加");
                return;
            }
        }
        mPresenter.checkTrackingNumber(result, deliveryEntity.getExpressDeliveryID());
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        deliveryEntity = (ExpressDeliveryEntity) getIntent().getSerializableExtra(IntentKey.OBJECT_KEY);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> trackingNumberList = new ArrayList<>();
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    trackingNumberList.add(mAdapter.getItem(i).getTrackingNumber());
                }
                mPresenter.confirmRegister(deliveryEntity.getExpressDeliveryID(), trackingNumberList);
            }
        });
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, ExpressDeliveryEntity item) {
        helper.setText(R.id.tv_member_name, item.getMemberID().equals("0") ? "" : item.getMemberName())
                .setText(R.id.tv_express_delivery_name, deliveryEntity.getExpressDeliveryName())
                .setText(R.id.tv_tracking_number, item.getTrackingNumber())
                .setText(R.id.tv_is_original_order, item.isOriginalOrder() ? "是" : "否");
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case ReturnRegisterPresenter.CHECK_TRACKING_NUMBER:
                ExpressDeliveryEntity entity = (ExpressDeliveryEntity) message.obj;
                if (entity.isOriginalOrder()) {
                    new VolumeManager(this).setVoice(100);
                    PlayVoiceManager.with(this).play(R.raw.original_bill);
                }
                entity.setTrackingNumber(trackingNumber);
                mAdapter.addData(0, entity);
                rvList.smoothScrollToPosition(0);
                btnConfirm.setVisibility(View.VISIBLE);
                break;
            case ReturnRegisterPresenter.CONFIRM_RETURN_REGISTER:
                success("登记成功");
                finish();
                break;
        }
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<ExpressDeliveryEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        new AlertDialogBuilder(this)
                .setTitle("删除确认")
                .setMessage("确认删除该面单吗？")
                .setPositiveButton("确认", (dialog, which) -> {
                    mAdapter.removeAt(position);
                    if (mAdapter.getData().size() == 0)
                        btnConfirm.setVisibility(View.GONE);
                })
                .setNegativeButton("取消", null)
                .show();
    }

    public static void jumpToNext(Activity activity, ExpressDeliveryEntity entity) {
        Intent intent = new Intent(activity, ReturnRegisterActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, entity);
        activity.startActivity(intent);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
